<!-- 采购管理-采购退货列表 -->
<template>
  <pageHeader :title="title" :breadCrumb="breadCrumb" :show-back="true">
    <template #buts>
      <a-button type="primary">
        <template #icon>
          <icon-plus></icon-plus>
        </template>
        <template #default>采购订单列表</template>
      </a-button>
    </template>
  </pageHeader>
  <div>
    <a-tabs @change="searchState" type="line">
      <a-tab-pane :key="0" title="全部申请"> </a-tab-pane>
      <a-tab-pane :key="1" title="我的申请"> </a-tab-pane>
    </a-tabs>
  </div>
  <div class="search-box" style="padding: 0px 15px">
    <div>
      <a-space>
        <a-input-search
          search-button
          :style="{ width: 'auto' }"
          placeholder="请输入"
          v-model="currSelectSearch"
        >
          <template #prefix>
            <a-select
              :style="{ width: '120px', marginRight: '10px' }"
              placeholder="请选择"
              v-model="currSelectVal"
              :default-value="currSelectVal"
            >
              <a-option value="number">采购申请单号</a-option>
              <a-option value="applyMan">申请人</a-option>
              <a-option value="applyDept"> 申请部门</a-option>
            </a-select>
          </template>
        </a-input-search>
        <a-select placeholder="请选择" v-model="userState" @change="search">
          <template #prefix>状态：</template>
          <a-option :value="1">待执行</a-option>
          <a-option :value="4">执行中</a-option>
          <a-option :value="3">审核中</a-option>
          <a-option :value="6">已完成</a-option>
          <a-option :value="5">不通过</a-option>
          <a-option :value="7">异常完成</a-option>
          <a-option :value="0">全部</a-option>
        </a-select>
        <a-select v-model="priorityData" placeholder="请选择" @change="search">
          <template #prefix>优先级：</template>
          <a-option :value="1">低</a-option>
          <a-option :value="2">中</a-option>
          <a-option :value="3">高</a-option>
          <a-option :value="4">紧急</a-option>
          <a-option :value="0">全部</a-option>
        </a-select>

        <a-range-picker showTime @change="onChange" style="width: 380px" />
      </a-space>
    </div>
    <div>
      <a-space>
        <a-button type="outline" @click="reset">
          <template #icon>
            <icon-refresh />
          </template>
          <template #default>重置</template>
        </a-button>
        <a-button status="success">
          <template #icon>
            <icon-download />
          </template>
          <template #default>导入</template>
        </a-button>

        <a-button status="warning">
          <template #icon>
            <icon-export rotate="-90" />
          </template>
          <template #default>导出</template>
        </a-button>

        <a-button type="primary" @click="showFormHeaderSetting"
          >表头设置</a-button
        >
      </a-space>
    </div>
  </div>
  <div style="padding: 10px">
    <tableTurn :tableData="getData" :args="args" ref="tableRef">
      <a-table-column :width="80" align="center">
        <template #title> 序号 </template>
        <template #cell="{ rowIndex }">
          {{ rowIndex + 1 }}
        </template>
      </a-table-column>
      <a-table-column
        :title="item.name"
        v-for="(item, index) in selectedHeader"
        :key="index"
        :data-index="item.code"
        align="center"
        :width="200"
        :sortable="{
          sortDirections: ['ascend', 'descend'],
        }"
      >
        <!-- 申请明细  -->
        <template v-if="item.code === 'applyDetail'" #cell="{ record }">
          <a-typography-text type="primary" @click="See(record.product)">
            查看
          </a-typography-text>
        </template>

        <!-- 申请原因 -->
        <template v-else-if="item.code === 'applyCause'" #cell="{ record }">
          <a-typography-text type="primary" @click="view(record.cause)">
            查看
          </a-typography-text>
        </template>
        <!-- 申请日期 -->
        <template v-else-if="item.code === 'apply_time'" #cell="{ record }">
          {{ dayjs(record.applyTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>

        <!-- 交付日期 -->
        <template v-else-if="item.code === 'delivery_time'" #cell="{ record }">
          {{ dayjs(record.applyTime).format('YYYY-MM-DD') }}
        </template>
        <!-- 优先级 -->
        <template v-else-if="item.code === 'priority'" #cell="{ record }">
          <a-typography-text
            v-if="record.priority === 1"
            style="color: #999999"
          >
            低
          </a-typography-text>
          <a-typography-text v-if="record.priority === 2" type="primary">
            中
          </a-typography-text>
          <a-typography-text v-if="record.priority === 3" type="warning">
            高
          </a-typography-text>
          <a-typography-text v-if="record.priority === 4" type="danger">
            紧急
          </a-typography-text>
        </template>

        <!-- 状态 -->
        <template v-else-if="item.code === 'state'" #cell="{ record }">
          <a-typography-text v-if="record.state === 1" style="color: #999999">
            待执行
          </a-typography-text>
          <a-typography-text v-if="record.state === 2" type="primary">
            执行中
          </a-typography-text>
          <a-typography-text v-if="record.state === 3" type="success">
            已完成
          </a-typography-text>
        </template>
      </a-table-column>
      <a-table-column :width="80" align="center">
        <template #title> <icon-settings /> </template>
        <template #cell="{ rowIndex }">
          {{ rowIndex + 1 }}
        </template>
      </a-table-column>
    </tableTurn>
  </div>
  <FormHeaderSetting
    v-model:visible="formHeaderSettingVisible"
    title="采购退货列表"
    :resultList="headerSetting.list"
    @headerSave="headerSave"
  ></FormHeaderSetting>
</template>

<script lang="ts" setup>
  import { reactive, ref, computed, onMounted } from 'vue';
  import { useRouter } from 'vue-router';
  import dayjs from 'dayjs';
  import {
    getPurchaseHeader,
    PUTPurchaseHeader,
    getPurchaseReturnList,
  } from '@/api/buy-management/buy-return/index';

  const router = useRouter();
  const title: string = '采购退货列表';
  const breadCrumb: string[] = ['采购管理', '采购管理', '采购退货列表'];
  const modelVisible = ref(false);
  const formHeaderSettingVisible = ref(false);
  let tableRef: any = ref(); // 储存table的ref
  let currSelectVal = ref(); // 储存当前下拉选择框的值
  let currSelectSearch = ref(''); // 储存下拉选择框对应的搜索框的值
  const modelCauseVisible = ref(false);
  const userState = ref();
  // 储存表头数据
  const priorityData = ref(); // 储存搜索优先级的值
  const headerSetting = reactive({
    list: [],
  });

  // 表格配置
  const args = reactive({
    stripe: true,
    rowSelection: {
      type: 'checkbox',
      showCheckedAll: true,
      onlyCurrent: false,
    },
    scroll: {
      y: 500,
    },
    bordered: {
      headerCell: true,
    },
    rowKey: 'name', // 批量选择按钮以客户名称来作为key
  });

  const showFormHeaderSetting = () => {
    formHeaderSettingVisible.value = true;
  };
  // 获取表头设置
  const headerSettingData = async () => {
    try {
      const res = await getPurchaseHeader({ userId: 317, purchaseType: 8 });
      headerSetting.list = res.extends;
    } catch (err: unknown) {
      console.log(err);
    }
  };
  headerSettingData();

  // 获取列表数据
  const getData = async (pageData?: any) => {
    try {
      const msg: any = {};
      if (currSelectVal.value !== '' && currSelectSearch.value !== '')
        msg[currSelectVal.value] = currSelectSearch.value;
      if (userState.value) msg.state = userState.value;
      if (priorityData.value) msg.priority = priorityData.value;
      const res = await getPurchaseReturnList({
        ...pageData,
        ...msg,
      });
      if (res.content === null && res.content === undefined) {
        res.content = [];
      }
      return res;
    } catch (err: unknown) {
      console.log(err);
    }
  };

  const search = () => {
    tableRef.value.loadData();
  };

  const reset = () => {
    currSelectVal.value = '';
    currSelectSearch.value = '';
    userState.value = undefined;
    priorityData.value = undefined;
    tableRef.value.loadData();
  };

  // 表头设置保存后返回的自定义事件
  const headerSave = async (val) => {
    try {
      const data = {
        userId: 317,
        purchaseType: 8,
        extend: JSON.stringify(val),
      };
      await PUTPurchaseHeader(data);
      headerSetting.list = [...val];
    } catch (err: unknown) {
      console.log(err);
    }
  };

  const SeeCauseDate = ref();
  const view = (content: string) => {
    SeeCauseDate.value = content;
    modelCauseVisible.value = true;
  };

  // 在表格上显示的表头
  const selectedHeader = computed(() => {
    const arr: any = [];
    headerSetting.list.forEach((item) => {
      if (item.isDefault === 1) {
        arr.push(item);
      }
    });
    return arr;
  });
  const SeeDate = ref();
  const See = (item: any) => {
    modelVisible.value = true;
    SeeDate.value = item;
  };
</script>

<style scoped lang="less">
  .search-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }

  :deep(.arco-space-item) {
    margin-top: 10px;
  }
</style>
